package com.example.administrator.xiaobo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.xiaobo.R;
import com.example.administrator.xiaobo.activity.FlowLayout;
import com.example.administrator.xiaobo.jsonbean.DadaMovie;
import com.example.administrator.xiaobo.jsonbean.MovieJsonBean;
import com.example.administrator.xiaobo.jsonbean.MovieUpData;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2017/2/11.
 */

public class BoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<DadaMovie> list;
    private LayoutInflater layoutInflater;
    public static final int ONE_ITEM = 1;
    public static final int TWO_ITEM = 2;
    public static final int Three_ITEM = 3;
    private boolean tags;

    public BoAdapter(Context context, List<DadaMovie> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
        for (DadaMovie dadaMovie : list) {
            if (dadaMovie instanceof MovieUpData) {

                tags = ((MovieUpData) dadaMovie).getData().getTags() == null;

                Log.e("DDDDDDDDDDDD", String.valueOf(tags));
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder holder = null;
        if (ONE_ITEM == viewType) {
            View v = layoutInflater.inflate(R.layout.movie_itemup, parent, false);
            holder = new OneViewHolder(v);
        } else if (TWO_ITEM == viewType) {
            View v = layoutInflater.inflate(R.layout.movie_item, parent, false);
            holder = new TwoViewHolder(v);
        } else {
            View v = layoutInflater.inflate(R.layout.item_l, parent, false);
            holder = new ThreeViewHolder(v);
        }
        return holder;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof OneViewHolder) {
            for (DadaMovie dadaMovie : list) {
                if (dadaMovie instanceof MovieUpData) {
                    if (tags) {
                        ((OneViewHolder) holder).name_movie_up.setText(((MovieUpData) dadaMovie).getData().getName() + "");
                        Glide.with(context).load(((MovieUpData) dadaMovie).getData().getAimage().getLevel1()).centerCrop().into(((OneViewHolder) holder).movie_image_up);
                        ((OneViewHolder) holder).name_movie_down.setText(((MovieUpData) dadaMovie).getData().getSource() + "");
                        ((OneViewHolder) holder).name_movie_p1.setText("在线：" + ((MovieUpData) dadaMovie).getData().getOnlinecount() + "");
                        ((OneViewHolder) holder).name_movie_p2.setText("粉丝：" + ((MovieUpData) dadaMovie).getData().getFans() + "");
                      ((OneViewHolder) holder).relati_Bo.setVisibility(View.GONE);

                    }else {
                        ((OneViewHolder) holder).name_movie_up.setText(((MovieUpData) dadaMovie).getData().getName() + "");
                        Glide.with(context).load(((MovieUpData) dadaMovie).getData().getAimage().getLevel1()).centerCrop().into(((OneViewHolder) holder).movie_image_up);
                        ((OneViewHolder) holder).name_movie_down.setText(((MovieUpData) dadaMovie).getData().getSource() + "");
                        ((OneViewHolder) holder).name_movie_p1.setText("在线：" + ((MovieUpData) dadaMovie).getData().getOnlinecount() + "");
                        ((OneViewHolder) holder).name_movie_p2.setText("粉丝：" + ((MovieUpData) dadaMovie).getData().getFans() + "");

                        List<MovieUpData.DataBean.TagsBean> mdatas=new ArrayList<>();
                        mdatas.addAll(((MovieUpData) dadaMovie).getData().getTags());

                        Log.e("DDDDD", String.valueOf(mdatas.size()));
                        Log.e("DDDDD", mdatas.get(0).getName());
                        Glide.with(context).load(((MovieUpData) dadaMovie).getData().getAimage().getLevel1()).centerCrop().into(((OneViewHolder) holder).iv_up);

                        ((OneViewHolder) holder).relati_Bo.setVisibility(View.VISIBLE);
                        intdata(holder,mdatas);
                    }
                }
            }

        } else if (holder instanceof ThreeViewHolder) {


        } else {

            for (DadaMovie dadaMovie : list) {
                if (dadaMovie instanceof MovieJsonBean) {

                    Glide.with(context).load(((MovieJsonBean) dadaMovie).getData().get(position - 2).getAimage().getLevel1()).centerCrop().into(((TwoViewHolder) holder).movie_image);

                    ((TwoViewHolder) holder).textView1.setText(((MovieJsonBean) dadaMovie).getData().get(position - 2).getLname());
                    ((TwoViewHolder) holder).textView2.setText(((MovieJsonBean) dadaMovie).getData().get(position - 2).getLiveplatform().getName());
                    ((TwoViewHolder) holder).textView3.setText(((MovieJsonBean) dadaMovie).getData().get(position - 2).getName());
                    ((TwoViewHolder) holder).textView4.setText(((MovieJsonBean) dadaMovie).getData().get(position - 2).getOnlinecount() + "");
                }
            }


        }

    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void intdata(RecyclerView.ViewHolder holder, List<MovieUpData.DataBean.TagsBean> mdatas) {


        Random random = new Random();

        // 循环添加TextView到容器
        for (int i = 0; i < mdatas.size(); i++) {
            final TextView view = new TextView(context);
            view.setText(mdatas.get(i).getName());
            view.setTextColor(Color.WHITE);
            view.setPadding(5, 5, 5, 5);
            view.setGravity(Gravity.CENTER);
            view.setTextSize(14);

            // 设置点击事件
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  //  Toast.makeText(MainActivity.this, view.getText().toString(), Toast.LENGTH_SHORT).show();
                }
            });

            // 设置彩色背景
            GradientDrawable normalDrawable = new GradientDrawable();
            normalDrawable.setShape(GradientDrawable.RECTANGLE);
            int a = 255;
            int r = 50 + random.nextInt(150);
            int g = 50 + random.nextInt(150);
            int b = 50 + random.nextInt(150);
            normalDrawable.setColor(Color.argb(a, r, g, b));

            // 设置按下的灰色背景
            GradientDrawable pressedDrawable = new GradientDrawable();
            pressedDrawable.setShape(GradientDrawable.RECTANGLE);
            pressedDrawable.setColor(Color.GRAY);

            // 背景选择器
            StateListDrawable stateDrawable = new StateListDrawable();
            stateDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable);
            stateDrawable.addState(new int[]{}, normalDrawable);

            // 设置背景选择器到TextView上
            view.setBackground(stateDrawable);
            ((OneViewHolder) holder).flowLayou.addView(view);

        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {

            return ONE_ITEM;
        } else if (position == 1) {
            return Three_ITEM;
        } else {
            return TWO_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        int postion = 0;

            for (DadaMovie dadaMovie : list) {
                if (dadaMovie instanceof MovieJsonBean) {

                    postion = ((MovieJsonBean) dadaMovie).getData().size() + 2;
                }

        }

        if (list == null && list.size() <= 0)
            return 0;
        return postion;
    }

    class OneViewHolder extends RecyclerView.ViewHolder {
        TextView name_movie_up;
        ImageView movie_image_up,iv_up;
        TextView name_movie_down;
        TextView name_movie_p1;
        TextView name_movie_p2;
        RelativeLayout relati_Bo;
        FlowLayout flowLayou;
        public OneViewHolder(View itemView) {
            super(itemView);
            name_movie_up = (TextView) itemView.findViewById(R.id.name_movie_up);
            movie_image_up = (ImageView) itemView.findViewById(R.id.movie_image_up);
            name_movie_down = (TextView) itemView.findViewById(R.id.name_movie_come);
            name_movie_p1 = (TextView) itemView.findViewById(R.id.tv_p1);
            name_movie_p2 = (TextView) itemView.findViewById(R.id.tv_p2);
            relati_Bo= (RelativeLayout) itemView.findViewById(R.id.relati_Bo);
            flowLayou= (FlowLayout) itemView.findViewById(R.id.tv_moive_yingxiang);
            iv_up = (ImageView) itemView.findViewById(R.id.iv_up);
        }
    }

    class TwoViewHolder extends RecyclerView.ViewHolder {

        ImageView movie_image;
        TextView textView1, textView2, textView3, textView4;

        public TwoViewHolder(View itemView) {
            super(itemView);
            movie_image = (ImageView) itemView.findViewById(R.id.movie_image);
            textView1 = (TextView) itemView.findViewById(R.id.name_up);
            textView2 = (TextView) itemView.findViewById(R.id.name_bellow);
            textView3 = (TextView) itemView.findViewById(R.id.name_bellow2);
            textView4 = (TextView) itemView.findViewById(R.id.movie_people);

        }
    }

    class ThreeViewHolder extends RecyclerView.ViewHolder {
            public ThreeViewHolder(View itemView) {
                super(itemView);

            }
    }
}
